﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProBugTracker.Data.Interface;

namespace ProBugTracker.Data
{
    public class RoleManagerData : IRoleManagerData
    {
        public bool AddNewRole(UserRoleDB role)
        {
            try
            {
                using (ProBugTrackerEntities proBugTrackerEntities = new ProBugTrackerEntities())
                {
                    proBugTrackerEntities.AddToUserRoleDBs(role);
                    proBugTrackerEntities.SaveChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool EditRoleName(int roleID, UserRoleDB role)
        {
            try
            {
                using (ProBugTrackerEntities proBugTrackerEntities = new ProBugTrackerEntities())
                {
                    UserRoleDB userRole = (from u in proBugTrackerEntities.UserRoleDBs where u.ID == roleID select u).FirstOrDefault();
                    userRole.RoleName = role.RoleName;
                    proBugTrackerEntities.SaveChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool RemoveRole(int roleID)
        {
            try
            {
                using (ProBugTrackerEntities proBugTrackerEntities = new ProBugTrackerEntities())
                {
                    UserRoleDB userRole = (from u in proBugTrackerEntities.UserRoleDBs where u.ID == roleID select u).FirstOrDefault();
                    proBugTrackerEntities.DeleteObject(userRole);
                    proBugTrackerEntities.SaveChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public IList<UserAssignRoleDB> FindUserAssignRoleByUserID(int roleID)
        {
            try
            {
                using (ProBugTrackerEntities proBugTrackerEntities = new ProBugTrackerEntities())
                {
                    return (from u in proBugTrackerEntities.UserAssignRoleDBs where u.UserID == roleID select u).ToList();

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }




        public UserRoleDB FindUserRoleByID(int roleID)
        {
            try
            {
                using (ProBugTrackerEntities proBugTrackerEntities = new ProBugTrackerEntities())
                {
                    return (from u in proBugTrackerEntities.UserRoleDBs where u.ID == roleID select u).SingleOrDefault();

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
